<?php

/*
Name: edit-details.php
Author: Justin Williamson [epicoxymoron@gmail.com]
Description: Editor include
Last Modified: 10/20/2008
*/

require_once "includes/config.php";
require_once INCLUDE_DIR."/status.inc.php";
require_once INCLUDE_DIR."/db.inc.php";

print_r( $_POST );
print_r( $_GET );

function deleteRequirement( $id ) {
	// delete the actual requirement
	$query = mysql_query( "DELETE FROM `requirement` WHERE `id` = ".$id ) or die( mysql_error() );
	deleteRequirementReferences( $id );
}

function deleteRequirementReferences( $id ) {
	// delete references
	$query = mysql_query( "DELETE FROM `references` WHERE `requirement_id` = ".$id ) or die( mysql_error() );
}

function deleteTestCase( $id ) {
	// delete the actual test case
	$query = mysql_query( "DELETE FROM `test_case` WHERE `id` = ".$id ) or die( mysql_error() );
	deleteTestCaseReferences( $id );
}

function deleteTestCaseReferences( $id ) {
	// delete references
	$query = mysql_query( "DELETE FROM `references` WHERE `test_case_id` = ".$id ) or die( mysql_error() );
}

if( $_POST[ 'action' ] == "delete" ) {
	if( $_POST[ 'type' ] == "requirement" ) {
		deleteRequirement( $_POST[ 'id' ] );
		header( "Location: requirements.php" );
	}
	elseif( $_POST[ 'type' ] == "test_case" ) {
		deleteTestCase( $_POST[ 'id' ] );
		header( "Location: test_cases.php" );
	}
}

elseif( $_POST[ 'action' ] == "edit" ) {
	if( $_POST[ 'save_changes' ] ) {
		if( $_POST[ 'type' ] == "requirement" ) {
			// create if necessary
			$query = mysql_query( "SELECT `id`, `comments` FROM `requirement` WHERE id = ".$_POST[ 'id' ] );
			$row = mysql_fetch_array( $query );
			if( mysql_num_rows( $query ) == 0 ) {
				mysql_query( "INSERT INTO `requirement` (`id`, `short`, `description`, `status`, `comments`) ".
							 "VALUES( NULL , 'preemptive', 'preemptive', '2', 'preemptive' )" );
			}
			
			if ( $row[ 'comments'] != "") {
				$extra = "<br /><br /><br />";
			}
			
			// update requirement details
			$query = mysql_query( "UPDATE `requirement` SET ".
								  "`short` = '".addslashes( $_POST[ 'req_short_desc' ] )."', ".
								  "`description` = '".addslashes( $_POST[ 'req_details' ] )."', ".
								  "`status` = ".$_POST[ 'req_status' ].", ".
								  "`comments` = '". $row[ 'comments' ] . $extra . addslashes( nl2br( $_POST[ 'req_comments' ] ) )."' ".
								  "WHERE id = ".$_POST[ 'id' ] ) or die( mysql_error() );
			// update references
			deleteRequirementReferences( $_POST[ 'id' ] );
			foreach( $_POST[ 'tc_references' ] as $ref ) {
				mysql_query( "INSERT INTO `references` (`test_case_id`, `requirement_id`) VALUES (".$ref.", ".$_POST[ 'id' ] .")" );
			}
			header( "Location: requirement_details.php?id=".$_POST[ 'id' ] );
		}
		elseif( $_POST[ 'type' ] == "test_case" ) {
			// create if necessary
			$query = mysql_query( "SELECT `id` FROM `test_case` WHERE id = ".$_POST[ 'id' ] );
			if( mysql_num_rows( $query ) == 0 ) {
				mysql_query( "INSERT INTO `test_case` (`id`, `short`, `description`) ".
							 "VALUES( NULL , 'preemptive', 'preemptive' )" );
			}
			// update test case details
			$query = mysql_query( "UPDATE `test_case` SET ".
								  "`short` = '".addslashes( $_POST[ 'tc_short_desc' ] )."', ".
								  "`description` = '".addslashes( $_POST[ 'tc_details' ] )."' ".
								  "WHERE id = ".$_POST[ 'id' ] ) or die( "$query ==> ".mysql_error() );
			// update references
			deleteTestCaseReferences( $_POST[ 'id' ] );
			foreach( $_POST[ 'rqd_references' ] as $ref ) {
				mysql_query( "INSERT INTO `references` (`test_case_id`, `requirement_id`) VALUES (".$_POST[ 'id' ].", ".$ref.")" );
			}
			header( "Location: test_case_details.php?id=".$_POST[ 'id' ] );
		}
	}
	// must be to delete, then, if not to save changes
	else {
		if( $_POST[ 'type' ] == "requirement" ) {
			deleteRequirement( $_POST[ 'id' ] );
			header( "Location: requirements.php" );
		}
		elseif( $_POST[ 'type' ] == "test_case" ) {
			deleteTestCase( $_POST[ 'id' ] );
			header( "Location: test_cases.php" );
		}
	}
}

?>